﻿@model Affiliation
@inject IMenuProvider Menu
@{
    ViewData["Title"] = "Affiliation: " + Model.Name;
    IReadOnlyList<Microsoft.AspNetCore.Identity.IUser> admin = ViewBag.Administrators;
    var userNames = admin.ToDictionary(k => k.Id, v => v.UserName);
    ILookup<int, string> userRoles = ViewBag.UserRoles;
    IReadOnlyList<VerifyCode> codes = ViewBag.VerifyCodes;
}

<h2 class="mt-2">@ViewData["Title"]</h2>

<div class="row">
    <div class="col-lg-4">
        <table class="table table-sm table-striped">
            <tr><th>ID</th><td>@Model.Id</td></tr>
            <tr><th>Shortname</th><td>@Model.Abbreviation</td></tr>
            <tr><th>Name</th><td>@Model.Name</td></tr>
            <tr><th>Country</th><td>@(Model.CountryCode ?? "(none)")</td></tr>
            <tr><th>Student email suffix</th><td>@(Model.EmailSuffix ?? "(none)")</td></tr>
            <tr><th>Logo</th><td><img src="~/images/affiliations/@(Model.Abbreviation).png" width="40" height="40" /></td></tr>
        </table>
    </div>
</div>

<h3 class="mt-4">Administrators</h3>

<div class="table-wrapper">
    <table id="tenant-admins" class="data-table table table-sm table-striped" style="width:auto">
        <thead>
            <tr role="row">
                <th scope="col" style="min-width:3em" class="sortable searchable">ID</th>
                <th scope="col" style="min-width:10em" class="sortable searchable">name</th>
                <th scope="col" style="min-width:5em" class="sortable searchable">roles</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var user in admin)
            {
                <tr asp-route="AccountProfile" asp-route-username="@user.UserName" role="row" data-toggle="gotoUrl">
                    <td use-a>u@(user.Id)</td>
                    <td use-a>@user.UserName</td>
                    <td use-a><tags color="secondary" list="@string.Join(',', userRoles[user.Id].OrderBy(a => a))" /></td>
                </tr>
            }
        </tbody>
    </table>
</div>

<h3 class="mt-4">Invitation Codes</h3>

<div class="table-wrapper">
    <table id="verify-codes" class="data-table table table-sm table-striped" style="width:auto">
        <thead>
            <tr role="row">
                <th scope="col" style="min-width:3em" class="sortable searchable">ID</th>
                <th scope="col" style="min-width:5em" class="sortable searchable">code</th>
                <th scope="col" style="min-width:9em" class="sortable searchable">time</th>
                <th scope="col" style="min-width:5em" class="sortable searchable"># of used</th>
                <th scope="col" style="min-width:5em" class="sortable searchable">owner</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var code in codes)
            {
                <tr role="row">
                    <td>@code.Id</td>
                    <td class="@(code.IsValid ? "" : "ignore")">@code.Code</td>
                    <td>@code.CreationTime</td>
                    <td>@code.RedeemCount</td>
                    <td>@userNames.GetValueOrDefault(code.UserId, "UNKNOWN") (u@(code.UserId))</td>
                </tr>
            }
        </tbody>
    </table>
</div>

<script>
    $().ready(function () {
        $('.data-table').DataTable({
            "paging": false,
            "searching": false,
            "ordering": true,
            "order": [],
            "info": false,
            "autoWidth": true,
            "language": {
                "searchPlaceholder": "filter table",
                "search": "_INPUT_",
            },
            'aoColumnDefs': [
                { aTargets: ['sortable'], bSortable: true },
                { aTargets: ['searchable'], bSearchable: true },
                { aTargets: ['_all'], bSortable: false, bSearchable: false }
            ],
        });
    });
</script>
